package leetcode;

import util.Tree;
import util.TreeNode;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0098.java
 * @Author : WangRuoyu
 * @Date 2023/11/11 22:30
 */


public class Q0098 {
    private boolean dfs(TreeNode node, long min, long max) {
        if (node == null) return true;
        if (node.val > min && node.val < max) {
            return dfs(node.left, min, node.val) && dfs(node.right, node.val, max);
        } else {
            return false;
        }
    }

    public boolean isValidBST(TreeNode root) {
        return dfs(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public static void main(String[] args) {
        Q0098 sl = new Q0098();
        int[] pre = {5, 1, 4, 3, 6};
        int[] in = {1, 5, 3, 4, 6};
        TreeNode root = Tree.createTreeByPreorderAndInorder(pre, in);
        System.out.println(sl.isValidBST(root));
    }
}